package fr.afcepf.ai89.esharing.managedbean;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.faces.bean.ViewScoped;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

import fr.afcepf.ai89.esharing.entity.Civilite;
import fr.afcepf.ai89.esharing.entity.Complement;
import fr.afcepf.ai89.esharing.entity.FrCommune;
import fr.afcepf.ai89.esharing.entity.FrCommunePK;
import fr.afcepf.ai89.esharing.entity.FrDepartement;
import fr.afcepf.ai89.esharing.entity.FrNoTel;
import fr.afcepf.ai89.esharing.entity.FrRegion;
import fr.afcepf.ai89.esharing.metier.BusinessApprovisionnerCriteres;
import fr.afcepf.ai89.esharing.metier.IBusinessApprovisionnerCriteres;

public class InscriptionManagedBean implements Serializable {
	private IBusinessApprovisionnerCriteres busApp = new BusinessApprovisionnerCriteres();

	// private List<Civilite> civilites;
	private SelectItem[] civilites;
	private int civId;
	private String nomUtilisateur;
	private String courriel;
	private String mdp;
	private String mdp2;
	private String nom;
	private String prenom;
	private String noVoie;
	private SelectItem[] complements;
	private int complId;
	private String nomRue;
	private SelectItem[] regions;
	private Integer regionId;
	private SelectItem[] departements;
	private String departementId;
	private SelectItem[] codePostaux;
	private Integer codePostalId;

	private SelectItem[] communes;

	private FrNoTel tel1 = new FrNoTel();
	private FrNoTel tel2 = new FrNoTel();
	private Date dateNaissance;
	private String autobiographie;
	private boolean accepte;
	private String captacha;
	
	private FrCommunePK communeId = new FrCommunePK();
	
	private int communeCode = 0;
	
	public int getCommuneCode() {
		return communeCode;
	}

	public void setCommuneCode(int communeCode) {
		this.communeCode = communeCode;
	}

	public FrCommunePK getCommuneId() {
		return communeId;
	}

	public void setCommuneId(FrCommunePK communeId) {
		this.communeId = communeId;
	}

	public InscriptionManagedBean(){
		departementId = null;
		loadDepartements();
		
	}

	public SelectItem[] getCivilites() {
		if (civilites == null) {
			List<Civilite> list = busApp.getCivilites();
			int i = 1;
			civilites = new SelectItem[list.size() + 1];
			civilites[0] = new SelectItem();
			civilites[0].setValue(null);
			civilites[0].setLabel("--");
			for (Civilite c : list) {
				civilites[i] = new SelectItem(c);
				civilites[i].setValue(c.getIdCivilite());
				civilites[i].setLabel(c.getCivilite());
				i++;
			}
		}
		return civilites;
	}

	public String valueChangeRegion() {

		if (regionId != null) {
			
			FrRegion region = new FrRegion();
			region.setIdRegion(regionId);
			List<FrDepartement> liste = busApp.getDepts(region);
			
			int i = 1;
			
			departements = new SelectItem[liste.size() + 1];
			departements[0] = new SelectItem();
			departements[0].setLabel("--");
			departements[0].setValue(null);
			
			for (FrDepartement dept : liste) {
				
				departements[i] = new SelectItem(dept.getIdDepartement(), String.valueOf(dept.getIdDepartement()) + " " + dept.getNomDepartement());
				i++;
				
			}
			
			for (i = 0; i < departements.length; i++) {
				
				//System.out.println(departements[i].getLabel());
				
			}
			
		}
		
		return "";
		
	}

	public String valueChangeDepartement() {

		if (departementId != null) {
			
			FrDepartement dept = new FrDepartement();
			dept.setIdDepartement(departementId);
			
			List<Integer> listeCodePostaux = busApp.getCodesPostaux(dept);
			loadCodePostaux(listeCodePostaux);
			
			List<FrCommune> listeCommunes = busApp.getCommunes(dept);
			loadCommunes(listeCommunes);
			
		}
		
		return "";
		
	}

	private void loadRegions() {
		List<FrRegion> liste = busApp.getRegions();
		
		Collections.sort(liste, new FrRegionComparateur());
		
		int i = 1;
		regions = new SelectItem[liste.size() + 1];
		regions[0] = new SelectItem();
		regions[0].setLabel("--");
		regions[0].setValue(null);
		for (FrRegion region : liste) {
			regions[i] = new SelectItem();
			regions[i].setLabel(region.getNomRegion());
			regions[i].setValue(region.getIdRegion());
			i++;
		}
	}

	private void loadDepartements() {
		List<FrDepartement> liste = busApp.getDepts();
		int i = 1;
		departements = new SelectItem[liste.size() + 1];
		departements[0] = new SelectItem();
		departements[0].setLabel("--");
		departements[0].setValue(null);
		for (FrDepartement dept : liste) {
			departements[i] = new SelectItem();
			
			departements[i].setLabel(String.valueOf(dept.getIdDepartement()) + " " + dept.getNomDepartement());
			departements[i].setValue(dept.getIdDepartement());
			i++;
		}
	}

	private void loadCodePostaux(List<Integer> liste) {
		
		int i = 1;
		
		Collections.sort(liste);
		
		codePostaux = new SelectItem[liste.size() + 1];
		
		codePostaux[0] = new SelectItem();
		codePostaux[0].setLabel("--");
		codePostaux[0].setValue(null);
		
		for (int cp : liste) {
			String cpString = String.valueOf(cp);
			if (cpString.length() < 5) {
				cpString = "0" + cpString;
			}
			codePostaux[i] = new SelectItem(cp, cpString);
			i++;
		}
	}

	private void loadCommunes(List<FrCommune> liste) {
		
		int i = 1;
		
		Collections.sort(liste, new FrCommuneComparateur());
		
		communes = new SelectItem[liste.size() + 1];
		communes[0] = new SelectItem();
		communes[0].setLabel("--");
		communes[0].setValue(null);
		
		for (FrCommune com : liste) {
			
			communes[i] = new SelectItem(com.getId().getCodeCommune(), com.getNomCommune());
			i++;
			
		}
		
	}

	public void valueChangeVille() {
		
		//System.out.println(departementId + communeCode);
		
		if (communeCode > 0) {
			
			FrCommune com = new FrCommune();
			com.setId(new FrCommunePK());
			com.getId().setCodeCommune(communeCode);
			com.getId().setIdDepartement(departementId);
			List<Integer> cps = busApp.getCodesPostaux(com);
			loadCodePostaux(cps);
			
		}
		
	}

	public void valueChangeCp() {
		
		if (codePostalId != null) {
			
			List<FrCommune> cps = busApp.getCommunes(codePostalId);
			loadCommunes(cps);
			
		}
		
	}

	public void setCivilites(SelectItem[] civilites) {
		this.civilites = civilites;
	}

	public String getNomUtilisateur() {
		return nomUtilisateur;
	}

	public void setNomUtilisateur(String nomUtilisateur) {
		this.nomUtilisateur = nomUtilisateur;
	}

	public String getCourriel() {
		return courriel;
	}

	public void setCourriel(String courriel) {
		this.courriel = courriel;
	}

	public String getMdp() {
		return mdp;
	}

	public void setMdp(String mdp) {
		this.mdp = mdp;
	}

	public String getMdp2() {
		return mdp2;
	}

	public void setMdp2(String mdp2) {
		this.mdp2 = mdp2;
	}

	public String getNom() {
		return nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}

	public String getPrenom() {
		return prenom;
	}

	public void setPrenom(String prenom) {
		this.prenom = prenom;
	}

	public String getNoVoie() {
		return noVoie;
	}

	public void setNoVoie(String noVoie) {
		this.noVoie = noVoie;
	}

	public String getNomRue() {
		return nomRue;
	}

	public void setNomRue(String nomRue) {
		this.nomRue = nomRue;
	}

	public FrNoTel getTel1() {
		return tel1;
	}

	public void setTel1(FrNoTel tel1) {
		this.tel1 = tel1;
	}

	public FrNoTel getTel2() {
		return tel2;
	}

	public void setTel2(FrNoTel tel2) {
		this.tel2 = tel2;
	}

	public Date getDateNaissance() {
		return dateNaissance;
	}

	public void setDateNaissance(Date dateNaissance) {
		this.dateNaissance = dateNaissance;
	}

	public String getAutobiographie() {
		return autobiographie;
	}

	public void setAutobiographie(String autobiographie) {
		this.autobiographie = autobiographie;
	}

	public boolean isAccepte() {
		return accepte;
	}

	public void setAccepte(boolean accepte) {
		this.accepte = accepte;
	}

	public String getCaptacha() {
		return captacha;
	}

	public void setCaptacha(String captacha) {
		this.captacha = captacha;
	}

	public String inscrire() {
		return "";
	}

	public SelectItem[] getComplements() {
		if (complements == null) {
			List<Complement> liste = busApp.getComplements();
			int i = 1;
			complements = new SelectItem[liste.size() + 1];
			complements[0] = new SelectItem();
			complements[0].setLabel("--");
			complements[0].setValue(null);
			for (Complement complement : liste) {
				complements[i] = new SelectItem(complement.getIdComplement(),
						complement.getComplement());
				i++;
			}
		}
		return complements;
	}

	public void setComplements(SelectItem[] complements) {
		this.complements = complements;
	}

	public IBusinessApprovisionnerCriteres getBusApp() {
		return busApp;
	}

	public void setBusApp(IBusinessApprovisionnerCriteres busApp) {
		this.busApp = busApp;
	}

	public int getCivId() {
		return civId;
	}

	public void setCivId(int civId) {
		this.civId = civId;
	}

	public int getComplId() {
		return complId;
	}

	public void setComplId(int complId) {
		this.complId = complId;
	}

	public SelectItem[] getRegions() {
		loadRegions();
		return regions;
	}

	public void setRegions(SelectItem[] regions) {
		this.regions = regions;
	}

	public SelectItem[] getDepartements() {
		
		return departements;
	}

	public void setDepartements(SelectItem[] departements) {
		this.departements = departements;
	}

	public SelectItem[] getCodePostaux() {
		if (departementId == null) {
			codePostaux = new SelectItem[0];
		}
		return codePostaux;
	}

	public void setCodePostaux(SelectItem[] codePostaux) {
		this.codePostaux = codePostaux;
	}

	public SelectItem[] getCommunes() {
		if (departementId == null) {
			communes = new SelectItem[0];
		}
		return communes;
	}

	public void setCommunes(SelectItem[] communes) {
		this.communes = communes;
	}

	public Integer getRegionId() {
		return regionId;
	}

	public void setRegionId(Integer regionId) {
		this.regionId = regionId;
	}

	public String getDepartementId() {
		return departementId;
	}

	public void setDepartementId(String departementId) {
		this.departementId = departementId;
	}

	public Integer getCodePostalId() {
		return codePostalId;
	}

	public void setCodePostalId(Integer codePostalId) {
		this.codePostalId = codePostalId;
	}

}
